<?php
require_once(dirname(__FILE__)."/../../../config.php");

/*! \brief
 *  This is WFLangFileManager class
 *  This class handles getting language xml file
 * \author Lucy Lu
 */
class WFLangFileManager {
    /**
     * @var WF
     */
    private $objFramework;
    public function __construct($objFramework){
        $this->objFramework = $objFramework;
    }
    public function getPathLangFile($strLang, $strFolder, $strLangType, $intUserGenerated){
        $strLangUrlPrefix = $this->getLangUrlPrefix($strFolder, $strLangType, $intUserGenerated);
		$strWFLang = $this->getLangFromMapping($strLang);
		$strLangFilePath = $this->getLangFilePath($strLangUrlPrefix, $strWFLang);
		$strDefaultLangFilePath = $this->getDefaultLangFilePath($strLangUrlPrefix);
		$ret = '';
		if(file_exists($strLangFilePath)){
		    $ret = $strLangFilePath;
		}else if (file_exists($strDefaultLangFilePath)){
		    $ret = $strDefaultLangFilePath;
		}else{
			$message = __CLASS__ . '::' . __FUNCTION__ . '()=>Load xml language files [' . $strLangFilePath . ' and '.$strDefaultLangFilePath.'] Failed!';
			throw new WFLangManagerException($message);
		}
		return $ret;
    }
	/**
	 *\brief Description
	 *  - This method can get language xml file
	 *  - If success, return file cotent; If fail, return Exception
	 *\par Variable
	 *   - $strLang: language, ex. en_US
	 *   - $strFolder: folder which contains language file, ex. modSample
	 *   - $strLangType: language type, ex. WIDGET_LANG
	 *   - $intUserGenerated: whether language file is located in userGenerated folder, ex. 0 or 1
	 */	
	public function getLangFile($strLang, $strFolder, $strLangType, $intUserGenerated){
		$strLangFilePath = $this->getPathLangFile($strLang, $strFolder, $strLangType, $intUserGenerated);
		$objHandle = fopen($strLangFilePath, "r");
		$strContents = fread($objHandle, filesize($strLangFilePath));
		fclose($objHandle);
		if ($strContents === false){
			$message = __CLASS__ . '::' . __FUNCTION__ . '()=>Load xml language files [' . $strLangFilePath . '] Failed!';
			throw new WFLangManagerException($message);
		}
   		return $strContents;
	}

	/**
	 *\brief Description
	 *  - This method can get language from lanaguage mapping
	 *  - If success, return lanaguage; If fail, return exception
	 *\par Variable
	 *   - $strLang: language, ex. en_US
	 */
	public function getLangFromMapping($strLang){
           if(isset($GLOBALS["wfconf_lang_mapping"][$strLang]) && $GLOBALS["wfconf_lang_mapping"][$strLang]){
               return $GLOBALS["wfconf_lang_mapping"][$strLang];
           }else{
               $message = __CLASS__ . '::' . __FUNCTION__ . '()=>lang parameter that is not allowed [' . $strLang . ']!';
            			throw new WFLangManagerException($message);
           }
	}

	/**
	 *\brief Description
	 *  - This method can get language url prefix
	 *  - If success, return lanaguage url prefix; If fail, return empty
	 *\par Variable
	 *   - $strFolder: folder which contains language file, ex. modSample
	 *   - $strLangType: language type, ex. WIDGET_LANG
	 *   - $intUserGenerated: whether language file is located in userGenerated folder, ex. 0 or 1
	 */
	private function getLangUrlPrefix($strFolder, $strLangType,$intUserGenerated){
		$isDirectoryTraversal = WF::getSecurityFactory()->getSanitize()->isDirectoryTraversal($strFolder);
		if(true === $isDirectoryTraversal){
			$message = __CLASS__ . '::' . __FUNCTION__ . '()=>folder parameter that is not allowed [' . $strFolder . ']!';
			throw new WFLangManagerException($message);
		}		
		if ($strLangType == "WF_LANG"){
			return $this->objFramework->getConfigFactory()->getConfig()->getDirLang();
		}else if ($strLangType == "PRODUCT_LANG"){
			return WP_WEBROOT_NO_SLASH."/widgetBase/".$strFolder."/lang";
		}else if ($strLangType == "NEW_PRODUCT_LANG"){      
			return $GLOBALS["wfconf_repositoryDir"]."/widgets_new/widgetBase/".$strFolder."/lang";
		}else if ($strLangType == "WIDGET_LANG"){
			if ($intUserGenerated == 1){
				$strWidgetDir = USER_GENERATED_WIDGET_DIR;
			}else{
				$strWidgetDir = $GLOBALS["wfconf_dir_modules"];
			}
			return $strWidgetDir."/".$strFolder."/lang";
        }else if ($strLangType == "COMPONENT_LANG"){
            return WP_WEBROOT_NO_SLASH."/widgetComponent/".$strFolder."/lang";
		}else{
			return "";
		}
	}

	/**
	 *\brief Description
	 *  - This method can get language file path
	 *  - return file path
	 *\par Variable
	 *   - $strLangUrlPrefix: language url prefix, ex. lang
	 *   - $strWFLang: language, ex. en_US
	 */			
	private function getLangFilePath($strLangUrlPrefix, $strWFLang){
		$isDirectoryTraversal = WF::getSecurityFactory()->getSanitize()->isDirectoryTraversal($strWFLang);
		if(true === $isDirectoryTraversal){
			$message = __CLASS__ . '::' . __FUNCTION__ . '()=>lang parameter that is not allowed [' . $strWFLang . ']!';
			throw new WFLangManagerException($message);
		}	
		return dirname(__FILE__)."/../../../../".$strLangUrlPrefix."/".$strWFLang.".xml";
	}

	/**
	 *\brief Description
	 *  - This method can get file path of default language
	 *  - return file path
	 *\par Variable
	 *   - $strLangUrlPrefix: language url prefix, ex. lang
	 */
	private function getDefaultLangFilePath($strLangUrlPrefix){
		return dirname(__FILE__)."/../../../../".$strLangUrlPrefix."/default.xml";
	}

	public function setXMLHeader(){
		$this->objFramework->getIOFactory()->getFile()->setXMLHeader();
	}
}
